package com.demo.example.hot100;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class FindThePassword {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String M=scanner.next();
        int N=scanner.nextInt();
        String k=scanner.next();
        //思考一下,截取所有子串，进行操作
        char operator=k.charAt(0); //操作的符号
        BigInteger max=new BigInteger("0");
        for(int i=0;i<M.length();i++){
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(M.charAt(i));
            for(int j=i+1;j<M.length();j++){
                stringBuilder.append(M.charAt(j));
                //接下来进行运算操作
                BigInteger bigInteger1=new BigInteger(stringBuilder.toString());
                BigInteger bigInteger2=new BigInteger(Integer.toString(N));
                if(operator=='-'){
                    bigInteger2=bigInteger2.negate();
                }
                if(operator=='+'){
                    BigInteger sum=bigInteger1.add(bigInteger2);
                    if(isSame(sum.toString())){
                        max=max.max(bigInteger1);
                    }
                }else if(operator=='-'){
                   BigInteger rest=bigInteger1.add(bigInteger2);
                   if(isSame(rest.toString())){
                       max=max.max(bigInteger1);
                   }
                }else if(operator=='*'){
                    BigInteger rest=bigInteger1.multiply(bigInteger2);
                    if(isSame(rest.toString())){
                        if(isSame(rest.toString())){
                            max=max.max(bigInteger1);
                        }
                    }
                }else{
                    //除法的情况
                    BigInteger rest=bigInteger1.divide(bigInteger2);
                    if(isSame(rest.toString())){
                        max=max.max(bigInteger1);
                    }
                }
            }
        }
        System.out.println(max.equals(0)?max:-1);
    }
    //判定函数，查看函数的每一位上是否都为同一个
    public static boolean isSame(String s){
        Set<Character> set= new HashSet<>();
        for(int i=0;i<s.length();i++){
            set.add(s.charAt(i));
        }
        return set.size()==1;
    }
}
